﻿using CSharpDesign.DAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace CSharpDesign.BLL
{
    public class OrderChkService
    {
        /// <summary>
        /// 查找订单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="OrderStite"></param>
        public static string Select_Orderid(string id)
        {
            string OrderStite;
            SqlHelper data = new SqlHelper();
            SqlDataReader dr;
            string sql = "select * from GouWuChe where Orderid='" + id + "'";
            dr = data.GetDataReader(sql);
            dr.Read();
            try
            {
                OrderStite = dr["OrderStite"].ToString();
                return OrderStite;
            }
            catch(Exception)
            {
                throw new Exception("订单不存在");
            }
        }
        /// <summary>
        /// 更新订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="OrderStite"></param>
        public static void Update_OrderStiteod(string id, string OrderStite)
        {
            SqlHelper data = new SqlHelper();
            SqlDataReader dr;
            string sql = "select * from GouWuChe where Orderid='" + id + "'";
            dr = data.GetDataReader(sql);
            dr.Read();
            OrderStite = dr["OrderStite"].ToString();

            if (OrderStite == "待发货")
            {
                string sql1 = "select * from dbo.DingDan where OrderID='" + id + "'";
                SqlDataReader drKuCun = data.GetDataReader(sql1);
                while (drKuCun.Read())
                {
                    data.RunSql("update  ShangPin set SPNum=SPNum-" + drKuCun["shuliang"].ToString() + ",ZongXiaoLiang=ZongXiaoLiang+" + drKuCun["shuliang"].ToString() + "  where SPID='" + drKuCun["SPID"].ToString() + "'");

                }
                data.RunSql("update GouWuChe set OrderStite='待收货'  where Orderid='" + id + "'");

            }
            else
            {
                throw new Exception("订单状态不正确当前不允许重复操作！");


            }
        }
    }
}